. operationc (); Else System. Out. println ( "Sorry! I don't know who you are! " + O. tostring ()); //.... } //.... This is not impossible, but it will not be very scalable in the future. If you want to change the operation on each type of object at a time today, you must modify it in many places. From the perspective of the object itself, the object is in a house, and the object says, "Don't try to judge it out of the house. You don't know who I am, then you will come in and visit me. I
, which affects what the Visitor should do and what to do after accessing a class.
Abstract element class:The interface or abstract class declares the access type of the visitor, which is defined by the parameters in the accept method in the program. Abstract elements generally have two types of Methods: one is its own business logic, and the other is the type of visitors allowed to access.
Element class:The accept method declared to implement the abstract element class is usually visitor. Vis
{get ;}
Public void SetTraversalType (TraversalType _ type) {traversaltype = _ type;} // set a traversal a type, if it's not set manually, DepthFirst will be chosen in default
Public abstract bool IsEmpty (); // property takes the place of IsEmpty ()
Public abstract bool IsLeaf ();
// Only Visit, needn' t queue
Public virtual void DepthFirstTraversal (IPrePostVisitor _ vis) // middle depthfirst traversal
{
// Use different visitors through _ vis to perform forward, backward, and central Trave
The Visitor Pattern is one of the 23 design patterns proposed by GoF and belongs to the behavior Pattern.According to the big talk design model, it is the most complicated and hard to understand model.Definition (derived from GoF Design Pattern): represents an operation that acts on each element in an object structure. It allows youDefine new operations for these elements without changing the element classes. It can be seen from the definition that the structure object is necessary to use the vi
The visitor pattern is one of the 23 design patterns proposed by gof and belongs to the behavior pattern. According to the big talk design model, it is the most complicated and hard to understand model. Definition (derived from gof design pattern): represents an operation that acts on each element in an object structure. It allows you to define new operations that act on these elements without changing the element classes. From the definition, we can see that the structure object is a prerequisi
Abstractelement {
///Receive visitor access to public
abstract void accept (Ivisitor visitor); The business logic of the c3/>//itself is public
abstract void dosomething ();
}
2, the element concrete realization class
The element concretely implements class A public class
Concreteelementa extends Abstractelement {
//itself's business logic public
void DoSomething () C9/>system.out.println ("This is element 1");
}
Receive visitor access publi
traversal a type, if it's not set manually, Depthfirst is chosen in defaultpublic abstract bool IsEmpty ()//property takes the place of IsEmpty () public abstract bool IsLeaf ();Only Visit, Needn ' t queuepublic virtual void Depthfirsttraversal (Iprepostvisitor _vis)//middle Depthfirst traversal{Using different visitors to _vis, sequential, and sequential traversalif (! IsEmpty ()){_vis. Previsit (this. Key);if (this. Degree>=1){if (this. degree>=2){for (UINT i=0;i{This[i]. Depthfirsttraversal
IntroducedConcept: represents an operation that acts on elements in an object's structure. It allows you to define new actions that act on these elements without changing the class of each element.Scenario: There are differences in internal attributes between entities, and the entity types often increase. They are called the same way, but the rules of the call change frequently.Cons: Special Content access classes for entities need to be known.Code:Entity base class Package Note.com.visitor; Pu
on, which is rarely abstracted from this role in a project.
Common code implementations for visitor patterns[Java]View Plaincopy
Abstract class Element {
public abstract Void Accept (Ivisitor visitor);
public abstract void dosomething ();
}
Interface Ivisitor {
public void Visit (ConcreteElement1 el1);
public void Visit (ConcreteElement2 el2);
}
Class ConcreteElement1 ext
parameters in the visit method in the program define which objects can be accessed.Visitor:Implement the method declared by the abstract visitor, which affects what the Visitor should do and what to do after accessing a class.Abstract element class:The interface or abstract class declares the access type of the visitor, which is defined by the parameters in the accept method in the program. Abstract elements generally have two types of Methods: one is its own business logic, and the other is th
define which objects can be accessed.
Visitor: Implements the method that the abstract visitor declares, which affects what the visitor does and does when he accesses a class.
Abstract element class: An interface or abstract class that declares which type of visitor access to accept is defined by the parameters in the Accept method. Abstract elements generally have two types of methods, part of their own business logic, and the other is what kind of visitors are allowed to receive access.
Eleme
.
Finally pinch, flexibility is very high.
Next, I'll talk about my shortcomings, Isaac:
Specific element access publishes details to the visitor, and does not conform to the Dimitri rule.
Then it is difficult to change the specific elements.
Finally, I accidentally violated the dependency inversion principle. A visitor relies on a specific element, not an abstract element, which destroys the dependency inversion principle.
Well, the pros and cons seem to be so
receive.
Element class:The accept method declared to implement the abstract element class is usually visitor. visit (this), which basically forms a pattern.
Structure object:An element container generally contains a container that contains multiple different classes and interfaces, such as List, Set, and Map. This role is rarely abstracted from a project.
Abstract element class
abstract class Element { public abstract void accept(IVisitor visito
types of visitors are allowed to receiveto access. element class:implementing the Accept method declared by the abstract element class, usually visitor.visit (this), has basically formed a stereotype. Structure object:a container for an element that typically contains a container that accommodates multiple different classes and interfaces, such as list, Set, map, and so on, is rarely abstracted from this role in the project. abstract Element ClassAbstract class Element {public abstract voi
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.